﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using AuthCenter_HuangJingtao.Read.Api.Application.Command.ReversoaLL;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AuthCenter_HuangJingtao.Read.Api.Application.CommandHandler.ReversoaLL
{
    public class UpdateReversoCommandHandler : IRequestHandler<UpdateReversoCommand, ApiResult<int>>
    {
        IBaseReponsitory<Reverso> Reverso;

        public UpdateReversoCommandHandler(IBaseReponsitory<Reverso> Reverso)
        {
            this.Reverso = Reverso;
        }

        public async Task<ApiResult<int>> Handle(UpdateReversoCommand request, CancellationToken cancellationToken)
        {
            var Tst = Reverso.GetQuery().Where(x => x.Reverso_Name == request.Reverso.Reverso_Name && x.Reverso_Id != request.Reverso.Reverso_Id && !x.Reverso_Isdel).ToList();
            if (Tst.Count > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "角色名称已存在",
                    Data = -1
                };
            }
            var list = await Reverso.GetQuery().FirstOrDefaultAsync(c => c.Reverso_Id == request.Reverso.Reverso_Id);

            list.Reverso_Id = request.Reverso.Reverso_Id;
            list.Reverso_Name= request.Reverso.Reverso_Name;
            list.Reverso_Groud= request.Reverso.Reverso_Groud;
            list.Reverso_Type= request.Reverso.Reverso_Type;
            list.Reverso_Hours= request.Reverso.Reverso_Hours;
            list.Reverso_ActionTime= request.Reverso.Reverso_ActionTime;
            list.Reverso_Resion= request.Reverso.Reverso_Resion;
            list.Reverso_Isdel= request.Reverso.Reverso_Isdel;
            list.Reverso_State= request.Reverso.Reverso_State;
            list.Reverso_Refuse=request.Reverso.Reverso_Refuse;
            await Reverso.UpdateAsync(list);
            return new ApiResult<int>
            {
                Code = ApiEnum.Success,
                Message = "添加成功",
                Data = 1
            };
        }
    }
}
